<?php

namespace App\Http\Middleware;

use App\Http\Logic\Api\AuditToken;
use Closure;
use Exception;
use Illuminate\Support\Facades\Log;

class AuthBearerToken
{

    /**
     * @throws Exception
     */
    public function handle($request, Closure $next)
    {
        try {
            $result = AuditToken::decryptToken(str_replace('Bearer ', '', $request->headers->get('authorization')));
            if (empty($result) || $result['exp_time'] < time()) {
                throw new Exception('token验证失败或者过期');
            }

            return $next($request);
        } catch (Exception $e) {
            Log::info('[登录失效，请重新登录]'.$e->getMessage().'line:'.$e->getLine().'.file:'.$e->getFile());
            return response([
                'code' => 201,
                'msg' => '登录失效，请重新登录',
                'data' => []
            ]);
        }
    }
}
